using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
namespace SymbolicComputation
{
    public class AlgebraTransform : Transform
    {
        public AlgebraTransform()
        {
            Match += new MatchDelegate(AlgebraTransform_Match);
        }

        bool AlgebraTransform_Match(SyntaxNode transformMe, SyntaxNode matchToMe)
        {
            return MatchAlgebra(transformMe ,matchToMe);
        }

        public static Boolean MatchAlgebra(SyntaxNode transformMe, SyntaxNode matchToMe)
        {
            if (matchToMe.Kind == SyntaxKind.InvocationExpression)
            {
                if (transformMe.Kind != SyntaxKind.InvocationExpression)
                {
                    return false;
                }
                InvocationExpressionSyntax iMatchToMe = (InvocationExpressionSyntax)matchToMe;
                InvocationExpressionSyntax iTransformMe = (InvocationExpressionSyntax)transformMe;
                if (iMatchToMe.Expression.ToString() != iTransformMe.Expression.ToString())
                {
                    return false;
                }
            }
            if (matchToMe.Kind == SyntaxKind.ParenthesizedExpression)
            {
                if (transformMe.Kind != SyntaxKind.ParenthesizedExpression)
                {
                    return false;
                }
            }
            if (matchToMe.Kind == SyntaxKind.NegateExpression)
            {
                if (transformMe.Kind != SyntaxKind.NegateExpression)
                {
                    return false;
                }
            }
            if (matchToMe.Kind == SyntaxKind.NumericLiteralExpression)
            {
                if (transformMe.Kind != SyntaxKind.NumericLiteralExpression)
                {
                    return false;
                }
                if (transformMe.GetFullText() != matchToMe.GetFullText())
                {
                    return false;
                }
            }

            Type matchToMeType = matchToMe.GetType();
            Type transformMeType = transformMe.GetType();


            if (matchToMeType.Name == "BinaryExpressionSyntax")
            {
                if (transformMeType.Name != "BinaryExpressionSyntax")
                {
                    return false;
                }
                if (matchToMe.Kind != transformMe.Kind)
                {
                    return false;
                }
            }
            return true;
        }

        public SyntaxNode TransformAlgebraicSyntaxNode(SyntaxNode transformMe, Transform transform)
        {
            return TransformAlgebraicSyntaxNode(transformMe, transform.matchToMe, transform.outPattern);
        }

        public static String TransformAlgebraicString(String transformMe, String inTransform)
        {
            AlgebraTransform transform = new AlgebraTransform();
            return transform.TransformString(transformMe, inTransform);
        }

        public static SyntaxNode TransformAlgebraicSyntaxNode(SyntaxNode transformMe, SyntaxNode matchToMe, SyntaxNode outPattern)
        {
            AlgebraTransform transform = new AlgebraTransform();
            return transform.TransformSyntaxNode(transformMe, matchToMe,outPattern);
        }
    }
}